﻿using Admin.Net.InformationPlatform.Entity;
using Furion;
using Furion.DependencyInjection;
using Furion.JsonSerialization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;

namespace Admin.Net.InformationPlatform.Const
{
    #region T100接口
    /// <summary>
    /// T100接口任务名称
    /// </summary>
    public class ApiT100Const
    {
        /// <summary>
        /// 提交生产任务接口
        /// </summary>
        public static string SjMesFppCommit = "SjMesFppCommit";
        /// <summary>
        /// 提交生产任务接口
        /// </summary>
        public static string SjMesFosCommit = "SjMesFosCommit";
    }
    /// <summary>
    /// T100Api地址
    /// </summary>
    public class ApiT100Url
    {
        /// <summary>
        /// 正式区
        /// </summary>
        public static string FormalUrl = "http://192.168.1.21/wtopprd/ws/r/awsp920";
        /// <summary>
        /// 测试区
        /// </summary>
        public static string TestUrl = "http://192.168.1.21/wtoptst/ws/r/awsp920";
        /// <summary>
        /// 默认网址
        /// </summary>
        public static string Url = "http://192.168.1.21/wtopprd/ws/r/awsp920";

        /// <summary>
        /// T100Api调用参数设置
        /// </summary>
        /// <param name="user"></param>
        /// <param name="ServiceName"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        public static ApiT100Input<object> RrestT100InputResult(TokenUser user, string ServiceName, object input)
        {
            var output = new ApiT100Input<object>
            {
                digiHeader = new DigiHeader
                {
                    digiDatakey = new DigiDatakey
                    {
                        Ent = user.Ent,
                        Site = user.Site
                    },
                    digiHost = new DigiHost
                    {
                        acct = user.UserId,
                        id = "SjMes",
                        ip = App.HttpContext.GetRemoteIpAddressToIPv4(),
                        lang = user.Languages,
                        timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString()
                    },
                    digiservice = new digiservice
                    {
                        name = ServiceName
                    }
                },
                digibody = new Digibody<object>
                {
                    stddata = new Stddata<object>
                    {
                        parameter = input
                    }
                }
            };
            return output;

        }
    }
    /// <summary>
    /// Newtonsoft.Json 实现
    /// </summary>
    public class NewtonsoftJsonSerializerProvider : IJsonSerializerProvider, ISingleton
    {
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="value"></param>
        /// <param name="jsonSerializerOptions"></param>
        /// <returns></returns>
        public string Serialize(object value, object jsonSerializerOptions = null)
        {
            return JsonConvert.SerializeObject(value, (jsonSerializerOptions ?? GetSerializerOptions()) as JsonSerializerSettings);
        }
        /// <summary>
        /// 反序列化字符串
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="json"></param>
        /// <param name="jsonSerializerOptions"></param>
        /// <returns></returns>
        public T Deserialize<T>(string json, object jsonSerializerOptions = null)
        {
            return JsonConvert.DeserializeObject<T>(json, (jsonSerializerOptions ?? GetSerializerOptions()) as JsonSerializerSettings);
        }
        /// <summary>
        /// 返回读取全局配置的 JSON 选项
        /// </summary>
        /// <returns></returns>
        public object GetSerializerOptions()
        {
            return App.GetOptions<MvcNewtonsoftJsonOptions>()?.SerializerSettings;
        }

        public object Deserialize(string json, Type returnType, object jsonSerializerOptions = null)
        {
            throw new NotImplementedException();
        }
    }


    #region 请求参数模板

    /// <summary>
    /// 请求参数
    /// </summary>
    public class ApiT100Input<T>
    {

        /// <summary>
        /// 单头信息
        /// </summary>
        [JsonProperty(PropertyName = "digi-header")]
        public DigiHeader? digiHeader { get; set; }
        /// <summary>
        /// 传输内容
        /// </summary>
        [JsonProperty(PropertyName = "digi-body")]
        public Digibody<T> digibody { get; set; }
    }
    /// <summary>
    /// 参数单头信息
    /// </summary>
    public class DigiHeader
    {
        /// <summary>
        /// 账套信息
        /// </summary>
        [JsonProperty(PropertyName = "digi-datakey")]
        public DigiDatakey? digiDatakey { get; set; }
        /// <summary>
        /// 请求头信息
        /// </summary>
        [JsonProperty(PropertyName = "digi-host")]
        public DigiHost digiHost { get; set; }
        /// <summary>
        /// MD5码 ，默认：f5458f5c0f9022db743a7c0710145903
        /// </summary>
        [JsonProperty(PropertyName = "digi-key")]
        public string digikey { get; set; } = "f5458f5c0f9022db743a7c0710145903";
        /// <summary>
        /// 服务端信息
        /// </summary>
        [JsonProperty(PropertyName = "digi-service")]
        public digiservice digiservice { get; set; }

        /// <summary>
        /// 类型,默认：sync
        /// </summary>
        [JsonProperty(PropertyName = "digi-type")]
        public string digitype { get; set; } = "sync";
    }
    /// <summary>
    /// 账套信息
    /// </summary>
    public class DigiDatakey
    {
        /// <summary>
        /// 企业编号
        /// </summary>
        [JsonProperty(PropertyName = "EntId")]
        public string Ent { get; set; } = "8";
        /// <summary>
        /// 据点
        /// </summary>
        [JsonProperty(PropertyName = "CompanyId")]
        public string Site { get; set; } = "SJGS";
    }
    /// <summary>
    /// 请求头信息
    /// </summary>
    public class DigiHost
    {
        /// <summary>
        /// 请求发起人账号
        /// </summary>
        public string acct { get; set; }
        /// <summary>
        /// 请求端产品编号
        /// </summary>
        public string id { get; set; }

        /// <summary>
        /// 请求端ip地址
        /// </summary>
        public string ip { get; set; }
        /// <summary>
        /// 语言
        /// </summary>
        public string lang { get; set; }
        /// <summary>
        /// 请求发产品
        /// </summary>
        public string prod { get; set; } = "SJMES";
        /// <summary>
        /// 时间戳：
        /// </summary>
        public string timestamp { get; set; }
        /// <summary>
        /// 版本，默认：1.0
        /// </summary>
        public string ver { get; set; } = "1.0";
    }
    /// <summary>
    /// 服务端
    /// </summary>
    public class digiservice
    {
        /// <summary>
        /// 服务端，默认：topprd
        /// </summary>
        public string id { get; set; } = "topprd";
        /// <summary>
        /// 服务端IP地址
        /// </summary>
        public string ip { get; set; } = "192.168.1.21";
        /// <summary>
        /// 服务名
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 产品信息，默认:T100
        /// </summary>
        public string prod { get; set; } = "T100";
        /// <summary>
        /// 版本
        /// </summary>
        public string srvver { get; set; } = "1.0";
    }
    /// <summary>
    /// 传输值body
    /// </summary>
    public class Digibody<T>
    {
        /// <summary>
        /// 具体传输参数
        /// </summary>
        [JsonProperty(PropertyName = "std_data")]
        public Stddata<T> stddata { get; set; }
    }
    /// <summary>
    /// 传输值数据
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Stddata<T>
    {
        /// <summary>
        /// 具体传输参数
        /// </summary>
        public T parameter { get; set; }
    }
    #endregion

    #region 返回参数模板
    /// <summary>
    /// 返回参数
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ApiT100Output<T>
    {
        /// <summary>
        /// 回传表头
        /// </summary>
        [JsonProperty(PropertyName = "digi-header")]
        public OutputDigiHeader digiheader { get; set; }
        /// <summary>
        /// 回传正文
        /// </summary>
        [JsonProperty(PropertyName = "digi-body")]
        public OutputDigiBody<T> digiBody { get; set; }

    }
    /// <summary>
    /// 回传表头
    /// </summary>
    public class OutputDigiHeader
    {
        /// <summary>
        /// 版本
        /// </summary>
        [JsonProperty(PropertyName = "digi-srvver")]
        public string? DigiSrvver { get; set; }

        /// <summary>
        /// 编号
        /// </summary>
        [JsonProperty(PropertyName = "digi-srvcode")]
        public string? DigiSrvcode { get; set; }

    }
    /// <summary>
    /// 回传正文
    /// </summary>
    public class OutputDigiBody<T>
    {
        /// <summary>
        /// 回传结果
        /// </summary>
        [JsonProperty(PropertyName = "std_data")]
        public StddataStdData<T> stddata { get; set; }
    }
    /// <summary>
    /// 回传内容
    /// </summary>
    public class StddataStdData<T>
    {
        /// <summary>
        /// 执行结果
        /// </summary>
        [JsonProperty(PropertyName = "execution")]
        public Execution execution { get; set; }

        /// <summary>
        /// 具体传输参数
        /// </summary>
        public T parameter { get; set; }
    }
    /// <summary>
    /// 执行结果
    /// </summary>
    public class Execution
    {
        /// <summary>
        /// 结果代码
        /// </summary>
        [JsonProperty(PropertyName = "code")]
        public string? Code { get; set; }
        /// <summary>
        /// SQL执行代码
        /// </summary>
        [JsonProperty(PropertyName = "sql_code")]
        public string? SqlCode { get; set; }
        /// <summary>
        /// 说明
        /// </summary>
        [JsonProperty(PropertyName = "description")]
        public string? Description { get; set; }
    }

    #endregion
    #endregion



}
